package bb.lanxing.lib.devices.ble.device;

import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import com.github.mikephil.charting.utils.Utils;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.base.SimpleBluetoothGattCallback;
import bb.lanxing.lib.devices.ble.BLEAttributes;
import bb.lanxing.lib.devices.ble.x1.X1DisplayData;
import bb.lanxing.lib.devices.ble.x1.X1Controller;
import bb.lanxing.lib.devices.core.ble.AbsBleDevice;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Calendar;

public class X1Client extends AbsBleDevice implements X1Controller {
    private X1DisplayData mLastDisplayData;
    private BluetoothGattCharacteristic workoutClearCharacteristic;
    private BluetoothGattCharacteristic workoutConfigCharacteristic;
    private BluetoothGattCharacteristic workoutDataCharacteristic;
    private BluetoothGattCharacteristic workoutResetCharacteristic;

    public X1Client(Context context, SmartDevice smartDevice) {
        super(smartDevice);
        init(context, smartDevice.getAddress(), new SimpleBluetoothGattCallback(this));
    }

    @Override
    public void onServicesDiscovered() {
        BluetoothGattService service = this.mBluetoothGatt.getService(BLEAttributes.BLE_X1_WRITER_SERVICE);
        if (service == null) {
            d("Not found service for XI");
            return;
        }
        this.workoutDataCharacteristic = service.getCharacteristic(BLEAttributes.BLE_X1_WORKOUT_DATA_CHARACTER);
        this.workoutResetCharacteristic = service.getCharacteristic(BLEAttributes.BLE_X1_WORKOUT_RESET_CHARACTER);
        this.workoutConfigCharacteristic = service.getCharacteristic(BLEAttributes.BLE_X1_USER_CONFIG_CHARACTER);
        this.workoutClearCharacteristic = service.getCharacteristic(BLEAttributes.BLE_X1_CLEAR_CHARACTER);
        this.mLastDisplayData = null;
        stop();
    }

    @Override
    public void set(int i, float f, int i2) {
        if (!isServicesDiscovered()) {
            return;
        }
        int i3 = Calendar.getInstance().get(11);
        int currentTimeMillis = (int) ((System.currentTimeMillis() * 1000) % 3600000000L);
        ByteBuffer allocate = ByteBuffer.allocate(12);
        allocate.order(ByteOrder.LITTLE_ENDIAN);
        allocate.put((byte) i3);
        allocate.putInt(currentTimeMillis);
        allocate.put((byte) i2);
        allocate.putShort((byte) f);
        allocate.putInt(i);
        enqueue(AbsBleDevice.Request.newWriteRequest(this.workoutConfigCharacteristic, allocate.array()));
    }

    @Override
    public void reset(X1DisplayData x1DisplayData) {
        ByteBuffer allocate = ByteBuffer.allocate(12);
        allocate.order(ByteOrder.LITTLE_ENDIAN);
        allocate.putInt(x1DisplayData.getCalorie());
        allocate.putInt((int) (x1DisplayData.getDistance() * 100.0d));
        allocate.putInt(x1DisplayData.getDuration() * 1024);
        enqueue(AbsBleDevice.Request.newWriteRequest(this.workoutResetCharacteristic, allocate.array()));
        this.mLastDisplayData = x1DisplayData;
    }

    @Override
    public void send(X1DisplayData x1DisplayData) {
        X1DisplayData x1DisplayData2 = this.mLastDisplayData;
        if (x1DisplayData2 == null) {
            reset(x1DisplayData);
            return;
        }
        double distance = x1DisplayData2 != null ? x1DisplayData2.getDistance() : Utils.DOUBLE_EPSILON;
        X1DisplayData x1DisplayData3 = this.mLastDisplayData;
        byte b = 0;
        int duration = x1DisplayData3 != null ? x1DisplayData3.getDuration() : 0;
        X1DisplayData x1DisplayData4 = this.mLastDisplayData;
        int calorie = x1DisplayData4 != null ? x1DisplayData4.getCalorie() : 0;
        int distance2 = (int) ((x1DisplayData.getDistance() - distance) * 100.0d);
        int calorie2 = x1DisplayData.getCalorie() - calorie;
        ByteBuffer allocate = ByteBuffer.allocate(20);
        allocate.order(ByteOrder.LITTLE_ENDIAN);
        allocate.put((byte) 8);
        allocate.putShort((short) (x1DisplayData.getSpeed() * 3.6d * 10.0d));
        allocate.putInt(distance2);
        allocate.putInt((x1DisplayData.getDuration() - duration) * 1024);
        allocate.putInt(calorie2);
        allocate.putShort((short) (x1DisplayData.getAltitude() + 1000.0d));
        allocate.put((byte) x1DisplayData.getHeartrate());
        if (x1DisplayData.getCadence() <= 256) {
            b = (byte) x1DisplayData.getCadence();
        }
        allocate.put(b);
        allocate.put((byte) x1DisplayData.getAvgCadence());
        enqueue(AbsBleDevice.Request.newWriteRequest(this.workoutDataCharacteristic, allocate.array()));
        this.mLastDisplayData = x1DisplayData;
    }

    @Override
    public void stop() {
        reset(new X1DisplayData());
        this.mLastDisplayData = null;
    }
}
